<?php
namespace App\Services\IntegralGoods;

use App\Services\IntegralGoods\Goods\GoodsCategoryLogic;
use App\Services\User\App;
use Faker\Provider\Base;


/**
 * Class GoodsCategory
 * @package App\Services\IntegralGoods
 */
class GoodsCategory
{
    use \App\Services\Goods\Traits\BaseCheckCate;
    /*艺货后台发布/变价积分商品时，对基本数据以及分类的验证*/
    protected function _checkData($data, $scene = ''): array
    {
        if(isset($data['cate_id'])){
            $checkCate = (new GoodsCategoryLogic())->checkReadCate(['id'=>$data['cate_id'], 'app_id'=>$data['app_id']]);
            if(!$checkCate){  return verify_err(null, '该商品分类的状态错误');  }
        }
        return auto_success($data, '检验数据正常');
    }
    //添加或修改商品分类
    private function _updateGoodsCate($data, bool $isEdit)
    {
        $data = $isEdit ? init_array($data,['app_id'=>'','other_app_read'=>'',
            'is_public'=>0,'name'=>' ','id'=>0,'profile_pic'=>'',
            'keywords'=>'','description'=>'','order'=>100]):init_array($data,['app_id'=>'','other_app_read'=>'','is_public'=>0,
            'name'=>' ', 'pid'=>0,'profile_pic'=>'','keywords'=>'','description'=>'','order'=>100]) ;
        $checkData = $this->_baseCheckCate($data, '', 'edit',
            '\App\Services\IntegralGoods\Goods\GoodsCategoryLogic', 'dbOneCateInfo');
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        if(isset($data['id'])){
            $res = (new GoodsCategoryLogic())->editGoodsCate($data);
        }else{
            $data['level'] = $data['pid'] ? $checkData['res']['level'] + 1 : 0;
            unset($data['token']);
            $data['create_time']=time();
            $res = (new GoodsCategoryLogic())->addGoodsCate($data);
        }
        return $res;
    }
    //添加商品分类
    public function addGoodsCate($data)
    {

        ValidateGoodsCategory::checkValidate($data,["id","app_id","is_public","name","pid","order"]);
        return $this->_updateGoodsCate($data,false);
    }
    //编辑商品分类
    public function editGoodsCate($data)
    {
        if(empty($data['app_id'])){
           $data['app_id']=11;
        }
        unset($data['token']);

        $valdate=['action'=>'edit','id','app_id'];
        ValidateGoodsCategory::checkValidate($data,["id","app_id"],true,$valdate);
        return $this->_updateGoodsCate($data,true);
    }
    //后台获取某商品分类的详细信息
    public function oneCateInfo($data): array
    {
        $data = init_array($data,['id'=>0,'app_id'=>0]);

        $valdate=['action'=>'oneCateInfo','app_id','id'];
        ValidateGoodsCategory::checkValidate($data,['id','app_id'],true,$valdate);
        return (new GoodsCategoryLogic())->oneCateInfo($data);
    }
    /*
        根据id数组 启用/禁用一个或多个分类
        父类被禁用，则其所有子类也被禁用，开启同理
    */
    public function changeCateStatus($data): array
    {
        $data = init_array($data,['id'=>0,'status'=>' ','app_id'=>'']);

        $valdate=['action'=>'changeCateStatus','app_id','id','status'];
        ValidateGoodsCategory::checkValidate($data,['id','app_id','status'],true,$valdate);
        return (new GoodsCategoryLogic())->changeCateStatus($data);
    }
    //获取商品分类列表
    public function goodsCateList($data): array
    {
        $data = init_array($data,['id'=>0,'status'=>' ','app_id'=>' ']);

        $valdate=['action'=>'goodsCateList','app_id','recommend'];
        ValidateGoodsCategory::checkValidate($data,['id','app_id','recommend'],true,$valdate);
        return (new GoodsCategoryLogic())->goodsCateList($data);
    }
    //删除一个或多个商品分类
    public function delCate($data): array
    {
        $data = init_array($data,['id'=>0,'status'=>' ','app_id'=>'']);

        $valdate=['action'=>'delCate','id','app_id'];
        ValidateGoodsCategory::checkValidate($data,['id','app_id'],true,$valdate);
        return (new GoodsCategoryLogic())->delCate($data);
    }

}
